package edu.neelsys.repository.college;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.util.StringUtils;

import com.mysema.query.hql.HQLQuery;
import com.mysema.query.hql.jpa.JPAQuery;
import com.mysema.query.types.Predicate;

import edu.neelsys.objectmodel.dbentites.Address;
import edu.neelsys.objectmodel.dbentites.College;
import edu.neelsys.objectmodel.dbentites.QAddress;
import edu.neelsys.objectmodel.dbentites.QCollege;
import edu.neelsys.objectmodel.dbentites.QCollegeAddress;
import edu.neelsys.objectmodel.dbentites.QCollegeCourse;
import edu.neelsys.objectmodel.dbentites.QCourse;
import edu.neelsys.objectmodel.queryviews.CollegeSearchDTO;
import edu.neelsys.objectmodel.queryviews.QCollegeSearchDTO;

/**
 * Implementation of custom entity query methods. The example here uses QueryDsl
 * way so that queries are fully java complaint. However, there is a possiblity
 * to use named queries or simple string quereis as well here.
 * 
 * To Understand how to implement quereis do a search on QueryDsl examples in
 * google.
 * 
 * @author madhu
 * 
 */
public class CollegeRepositoryImpl implements CollegeRepoCustom {

	@PersistenceContext
	private EntityManager entityManager;

	public List<CollegeSearchDTO> findCollege(Address collegeDTO,
			String courseShortName) throws Exception {

		HQLQuery query = new JPAQuery(entityManager);

		QAddress address = QAddress.address1;
		QCollege college = QCollege.college;
		QCourse course = QCourse.course;
		QCollegeAddress collegeAddress = QCollegeAddress.collegeAddress;
		QCollegeCourse collegeCourse = QCollegeCourse.collegeCourse;

		query.from(college, address, course);

		Predicate collegeIdPredicate = college.id.eq(collegeAddress.collegeid
				.toString());
		Predicate addressIdPredicate = address.id.eq(collegeAddress.addressid
				.toString());
		Predicate collegeIdPredicate2 = collegeCourse.collegeid
				.eq(collegeAddress.collegeid);
		Predicate courseIdPredicate = course.id.eq(collegeCourse.courseid
				.toString());

		query.where(collegeIdPredicate, addressIdPredicate,
				collegeIdPredicate2, courseIdPredicate);

		if (collegeDTO.getAddress() != null) {
			Predicate addressPredicate = address.address.like(collegeDTO
					.getAddress());
			query.where(addressPredicate);
		}

		if (collegeDTO.getCity() != null) {
			Predicate cityPredicate = address.city.like(collegeDTO.getCity());
			query.where(cityPredicate);
		}
		if (collegeDTO.getCountry() != null) {
			Predicate countryPredicate = address.country.like(collegeDTO
					.getCountry());
			query.where(countryPredicate);
		}
		if (courseShortName != null) {
			Predicate courseNamePredicate = course.shortname
					.like(courseShortName);
			query.where(courseNamePredicate);
		}

		List<CollegeSearchDTO> collegeList = query.list(new QCollegeSearchDTO(
				college, address, course));

		return collegeList;
	}

}
